<!--选择公式-->
<template>
  <el-dialog :title="title" :visible.sync="visible" :width="width" :close-on-click-modal='false' class="bonus">
    <el-row>
      <el-form :model="formData" ref="formRef" :label-width="labelWidth"  :rules="rules" v-loading="loading" :close-on-click-modal="false">
        <el-col :span="24">
          <el-form-item label="公式" prop="gsId">
            <el-select v-model="formData.gsId" placeholder="请选择公式" clearable class="w100"
                       @change="formulaChange()">
<!--              <el-option :label="'平均分配'" :value="-1">
              </el-option>-->
              <el-option v-for="(item,index) in formulaOptions " :key="index" :label="item.gsMc" :value="item.id">
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="算法展示" prop="gsZs">
<!--            <el-tooltip class="item" effect="dark" :content="formData.gsZs" placement="bottom-start" v-if="formData.gsZs">-->
              <el-input  type="textarea"
                         :rows="3" v-model="formData.gsZs" placeholder="算法展示" disabled>   </el-input>
<!--            </el-tooltip>-->

<!--            <el-input v-else v-model="formData.gsZs" placeholder="算法展示" disabled>   </el-input>-->
          </el-form-item>
        </el-col>

      </el-form>
    </el-row>
    <div slot="footer" class="dialog-footer">
      <el-button @click="handleCancel(false)">取消</el-button>
      <el-button type="primary"  @click="handleSubmit">确定</el-button>
    </div>
  </el-dialog>
</template>

<script>
import {getJjDeptJjgtoPrant} from "@/api/admin/sysformula";

export default {
  name: "addForm",
  components:{

  },
  props:{
     loading:{
       type:Boolean,
       default:false
     }
  },
  data(){
    return{
      type:'add',
      title:'选择公式',
      width:'30%',
      labelWidth:'90px',
      visible: false,
      formData: {bonusItemDeptId:''},
      formulaOptions:[],
      rules:{
        gsId:[{ required: true, message: "请选择公式", trigger: "change" }],
      }
    }
  },
  methods:{
    init(type,data){
      this.type = type ; //type => ry | jg
      if (data){
        this.formData = data ; //公式信息回显
      }
      //获取公式列表
      this.getFormulaOption(type) ;

      this.visible = true ;

    },
    handleSubmit(){
      this.$refs.formRef.validate((valid) => {
        if (valid) {
          this.$emit('sendFormulaInfo',this.formData)
        } else {
          return false;
        }
      });
    },
    handleCancel(refresh){
      this.visible = false ;
      this.formData = {} ;
      if (refresh){
        this.$emit('refresh')
      }
    },
    //公式字典
    getFormulaOption(type) {
      let params = {} ;
      //如果人员和机构的公示不一样，用下面的代码
      // if(type=='ry'){
      //    params={
      //     id: this.deptId,
      //     gsType: '奖金算法',
      //     gsFl: '个人'
      //   }
      // }
      // if(type=='jg'){
      //    params={
      //     id: this.deptId,
      //     getJJxNamesType: '奖金算法',
      //     gsFl: '部门'
      //   }
      // }
      getJjDeptJjgtoPrant(params).then(res => {
        this.formulaOptions = res.data.data;
        this.formulaOptions.forEach(item=>{
          if(this.formData.gsId == item.id){
            this.$set(this.formData,'gsZs',item.gsZs)
          }
        })
        this.formulaChange() ;
      })
    },
    formulaChange(){
      if(this.formData.gsId==''){
        this.$set(this.formData,'gsZs','')
        this.$set(this.formData,'gsMc','')
      }else{
        this.formulaOptions.forEach(item=>{
          if(this.formData.gsId == item.id){
            this.$set(this.formData,'gsZs',item.gsZs)
            this.$set(this.formData,'gsMc',item.gsMc)
          }
        })
      }
    },
  }
}
</script>

<style scoped>

</style>
